<?php

/**
 * 好友邀请注册
 * File Name：MembersInviteModel.php
 * File Encoding：UTF-8
 * File New Time：2014-9-3 14:55:54
 * Author：水平凡
 * Mailbox：admin@abc3210.com
 */
class MembersInviteModel extends BaseModel {

    //返回表名
    public function tableName() {
        return '{{members_invite}}';
    }

    public function rules() {
        return array(
            array('uid', 'required', 'message' => 'UID不能为空！'),
            array('invite_uid', 'required', 'message' => '邀请注册用户UID不能为空！'),
            array('invite_username', 'required', 'message' => '邀请注册用户名不能为空！'),
            array('status,time', 'safe'),
        );
    }

    //数据保存前置操作
    protected function beforeSave() {
        if ($this->isNewRecord) {
            $this->time = time();
        }
        return true;
    }

    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * 被邀请注册的用户激活后给邀请人赠送积分
     * @param type $model 被邀请人会员信息对象
     * @return boolean
     */
    public function inviteActivate($model) {
        if (empty($model)) {
            return false;
        }
        $info = $this->find($this->where(array('invite_uid' => $model->uid, 'status' => 0)));
        if (empty($info)) {
            return false;
        }

        return $this->sendInvite($info, $model);
    }

    /**
     * 执行邀请好友积分赠送
     * 一天最多只能邀请5个好友，其它的则忽略了。
     * @param type $uid uid
     * @param type $model 被邀请注册的用户信息对象
     * @return boolean
     */
    public function sendInvite($uid, $model) {
        if (!is_object($uid)) {
            $info = $this->find($this->where(array('uid' => $uid, 'invite_uid' => $model->uid ? $model->uid : $model->primaryKey)));
        } else {
            $info = $uid;
            $uid = $info->uid;
        }
        if (empty($info) || $info->status) {
            return false;
        }

        //一天最多5个用户
        $time = strtotime('today');
        $count = $this->count($this->where(array('uid' => $uid, 'status' => 1, 'time' => array('GT', $time))));
        if ($count >= 5) {
            $info->status = 1;
            $info->save(false, array('status'));
            return false;
        }

        $info->status = 1;
        if ($info->save() && MembersModel::model()->plusScore($uid, 50)) {
            MembersActionModel::model()->addInviteAction($uid, '成功邀请好友 ' . $model->username . ' 注册赠送 50 积分。');
            //经验值
            MembersModel::model()->plusExp($uid, 10, '成功邀请好友 ' . $model->username . ' 注册，经验值加10。');
            return true;
        }
        return false;
    }

    /**
     * 增加邀请记录
     * @param type $uid 用户UID
     * @param type $invite_uid 被邀请用户UID
     * @return boolean
     */
    public function addInviteInfo($uid, $invite_uid) {
        $userInfo = MembersModel::model()->findByPk($invite_uid);
        if (empty($userInfo)) {
            return false;
        }
        $this->uid = $uid;
        $this->invite_uid = $invite_uid;
        $this->invite_username = $userInfo->username;
        $this->status = 0;
        $this->setIsNewRecord(true);
        if ($this->validate() && $this->save()) {
            return true;
        }
        return false;
    }

}
